<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/11
 * Time: 16:28
 */

namespace App\Models;


use App\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Permission extends Model
{
    use HasDateTimeFormatter;
    protected $table = 'admin_permissions';
    /**
     * @var array
     */
    protected $fillable = ['parent_id', 'name', 'slug', 'http_method', 'http_path','order'];


    /**
     * Permission belongs to many roles.
     *
     * @return BelongsToMany
     */
    public function roles(): BelongsToMany
    {
        $pivotTable = 'admin_role_permissions';

        $relatedModel = Role::class;

        return $this->belongsToMany($relatedModel, $pivotTable, 'permission_id', 'role_id');
    }

    /**
     * @return BelongsToMany
     */
    public function menus(): BelongsToMany
    {
        $pivotTable = 'admin_permission_menu';

        $relatedModel = Menu::class;

        return $this->belongsToMany($relatedModel, $pivotTable, 'permission_id', 'menu_id')->withTimestamps();
    }

}
